package edu.cug.water.crawler.job.entity.config;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.parser.deserializer.ParseProcess;
import com.alibaba.fastjson.serializer.ValueFilter;
import edu.cug.water.crawler.job.entity.committee.SongLiaoWater;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SongLiaoTimeFormatter implements ObjectDeserializer {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    private static String convertToStandardDateStr(String timeString) throws ParseException {
        // 获取当前年份和月份
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1; // 月份是从0开始的，所以要加1

        // 将时间字符串中的日期和小时提取出来
        int day = Integer.parseInt(timeString.substring(0, 2));
        int hour = Integer.parseInt(timeString.substring(3, 5));

        // 使用SimpleDateFormat将提取出的日期和小时组合成标准的日期字符串
        return String.format("%04d-%02d-%02d %02d:00:00", currentYear, currentMonth, day, hour);
    }

    @Override
    public String deserialze(DefaultJSONParser defaultJSONParser, Type type, Object name) {
        Object value = defaultJSONParser.parse();
        try {
            if ("tm".equals(name)) {
                return convertToStandardDateStr(value.toString());
            } else if ("stnm".equals(name)) {
                return value.toString().trim();
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
